#include <graphics.h>
#include <conio.h>
#include<math.h>
int main()
{
	initgraph(640,480);
	int ball_x[15],ball_y[15];
	int v_x[15],v_y[15];
	int i,j;

	for (i=0;i<15;i++) 
	{
		ball_x[i]=rand()%600+20;
		ball_y[i]=rand()%400+20;
	} 
	for (i=0;i<15;i++) 
	{
		v_x[i]=rand()%10;
		v_y[i]=rand()%10;
	} 

	BeginBatchDraw();

	while (1) 
	{

		for (i=0;i<15;i++)
		{
			setcolor(YELLOW);
			setfillcolor(GREEN);
			fillcircle(ball_x[i],ball_y[i],20);
		}

		FlushBatchDraw();
		Sleep(50);

		for(i=0;i<15;i++)
		{
			setcolor(BLACK);
			setfillcolor(BLACK);
			fillcircle(ball_x[i],ball_y[i],20);
		}

		for(i=0;i<15;i++)
		{
			if (ball_x[i]>=630 || ball_x[i]<=10)
				v_x[i]=-v_x[i];
			if (ball_y[i]>=470 || ball_y[i]<=10)
				v_y[i]=-v_y[i];
			ball_x[i]=ball_x[i]+v_x[i];
			ball_y[i]=ball_y[i]+v_y[i];
		}
		for(i=0;i<15;i++)
		{
			for(j=0;j<15;j++)
			{
				if(i!=j)
				{
					if(sqrt((ball_x[i] - ball_x[j])*(ball_x[i] - ball_x[j]) + (ball_y[i] - ball_y[j])*(ball_y[i] - ball_y[j])) <=2*20)
					{
						v_x[i]=-v_x[i];
						v_y[i]=-v_y[i];
						v_x[j]=-v_x[j];
						v_y[j]=-v_y[j];

						ball_x[i]=ball_x[i]+v_x[i];
						ball_y[i]=ball_y[i]+v_y[i];
						ball_x[j]=ball_x[j]+v_x[j];
						ball_y[j]=ball_y[j]+v_y[j];
					}
				}
			}
		}
	}
	EndBatchDraw();
	getch();
	closegraph();
	return 0;
}